package com.zyj.eps.controller;

import com.alibaba.fastjson.JSON;
import com.zyj.eps.pojo.*;
import com.zyj.eps.service.ManagerServiceImpl;
import com.zyj.eps.util.IDutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author super
 * @date 2022/4/3 17:34
 */
@Controller
@RestController
@CrossOrigin
@SuppressWarnings("all")//抑制警告
public class ManagerController {
    @Autowired
    private ManagerServiceImpl managerService;

    @RequestMapping("/getManagerById")
    public String getManagerById(int mid) {
        Manager managerById = managerService.getManagerById(mid);
        return JSON.toJSONString(managerById);
    }

    @RequestMapping("/updateManagerInfo")
    public String updateManagerInfo(@RequestBody Manager manager) {
        int i = managerService.updateManagerInfo(manager);
        String flag = "error";
        if (i == 1) {
            flag = "ok";
        }
        Manager managerById = managerService.getManagerById(manager.getMid());
        HashMap<String, Object> map = new HashMap<>();
        map.put("flag", flag);
        map.put("Manager", managerById);
        return JSON.toJSONString(map);
    }

    @RequestMapping("/updateUserHealth")
    public String updateUserHealth(@RequestBody User user) {
        int i = managerService.updateUserHealth(user);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }

    @RequestMapping("/Search")
    public String Search(@RequestBody Utils utils) {
        System.out.println(utils);
        if (utils.getNum() != 0) {
            List<User> list = managerService.searchUserById(utils.getNum());
            return JSON.toJSONString(list);
        } else {
            List<User> list1 = managerService.searchUserByName(utils.getInfo());
            List<User> list2 = managerService.searchUserByHealth(utils.getInfo());
            list1.addAll(list2);
            return JSON.toJSONString(list1);
        }
    }
    @RequestMapping("/updateGoodsNum")
    public String updateGoodsNum(@RequestBody Utils utils){
        System.out.println(utils);
        int i = managerService.updateGoodsNum(utils.getId(), utils.getNum());
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
    @RequestMapping("/getAllRecord")
    public String getAllRecord(){
        List<Record> allRecord = managerService.getAllRecord();
        return JSON.toJSONString(allRecord);
    }
    @RequestMapping("/searchRecordByUser")
    public String searchRecordByUser(String uname){
        List<Record> records = managerService.searchRecordByUser(uname);
        return JSON.toJSONString(records);
    }
    @RequestMapping("/ManagerAllVaccine")
    public String ManagerAllVaccine(){
        List<Vaccine> managerAllVaccine = managerService.ManagerAllVaccine();
        Collections.reverse(managerAllVaccine); //实现list集合逆序排列
        return JSON.toJSONString(managerAllVaccine);
    }
    @RequestMapping("/deleteVaccine")
    public String deleteVaccine(int vid){
        int i = managerService.deleteVaccine(vid);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
    @RequestMapping("/addVaccine")
    public String addVaccine(@RequestBody Vaccine vaccine){
        int i = managerService.addVaccine(vaccine);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
    @RequestMapping("/insertNews")
    public String insertNews(@RequestBody News news){
        news.setNewstime(new Date());
        int i = managerService.insertNews(news);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
    @RequestMapping("/deleteNews")
    public String deleteNews(int newsid){
        int i = managerService.deleteNews(newsid);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
    @RequestMapping("/getAllManager")
    public String getAllManager(){
        List<Manager> allManager = managerService.getAllManager();
        return JSON.toJSONString(allManager);
    }
    @RequestMapping("/addManager")
    public String addManager(@RequestBody Manager manager){
        System.out.println(manager);
        manager.setManagerID(IDutil.GetUUID());
        int i = managerService.addManager(manager);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
    @RequestMapping("/deleteManagerById")
    public String deleteManagerById(int mid){
        int i = managerService.deleteManagerById(mid);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
    @RequestMapping("/insertInform")
    public String insertInform(@RequestBody Inform inform){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        inform.setInformTime(sdf.format(new Date()));
        int i = managerService.insertInform(inform);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
    @RequestMapping("/deleteKnowledgeById")
    public String deleteKnowledgeById(int id){
        int i = managerService.deleteKnowledgeById(id);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
    @RequestMapping("/addKnowLedge")
    public String addKnowLedge(@RequestBody Knowledge knowledge){
        int i = managerService.addKnowLedge(knowledge);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
}
